package com.ssish.saas.eccs.service;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.ssish.saas.eccs.dto.BenefitDto;
import com.ssish.saas.eccs.dto.BenefitInfoDto;
import com.ssish.saas.eccs.dto.CoverageDto;
import com.ssish.saas.eccs.mapper.*;
import com.ssish.saas.eccs.model.*;
import com.ssish.saas.eccs.util.DateUtils;
import com.ssish.saas.eccs.util.HttpClientUtil;
import com.ssish.saas.eccs.vo.BenefitShowVo;
import com.ssish.saas.eccs.vo.CoverageVo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BenefitSerivice {
    @Autowired
    private EmployeeVoMapper employeeVoMapper;
    @Autowired
    private EmployeePlanMapper employeePlanMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BenefitVoMapper benefitVoMapper;
    @Autowired
    private SymUsrcoMapper symUsrcoMapper;
    @Autowired
    private ProviderMapper providerMapper;
    @Autowired
    private PlanConditionMapper planConditionMapper;
    @Autowired
    private CorporateMapper corporateMapper;
    @Autowired
    private EmpPlanViewMapper empPlanViewMapper;
    @Autowired
    private CardMapper cardMapper;
    /**
     * 查询会员列表
     * @param benefitDto
     * @return
     */
    public List<BenefitShowVo> getMemberInfo(BenefitDto benefitDto){
        return employeeVoMapper.selectEmployeeInfoByCondition(benefitDto);
    }

    /**
     * 查询保障计划
     * @param emPlanId
     * @return
     */
    public EmployeePlan getEmployeePlanInfoById(Long emPlanId){
        EmployeePlanKey employeePlanKey = new EmployeePlanKey();
        employeePlanKey.setEmpPlanId(emPlanId);
        EmployeePlan employeePlan =  employeePlanMapper.selectByPrimaryKeyNoData(employeePlanKey);
        if(new Date().compareTo(employeePlan.getStartDate())<0){
            return null;
        }
        if(employeePlan.getEndDate() != null && new Date().compareTo(employeePlan.getEndDate())>0){
            return null;
        }
        /*if(employeePlan.getCancellationDate() != null && new Date().compareTo(employeePlan.getCancellationDate())>0){
            return null;
        }*/
        return employeePlan;
    }

    /**
     * 获取最新的保障信息
     * @param emPlanId
     * @return
     */
    public EmpPlanView getEmpPlanViewInfoById(Long emPlanId){
        EmpPlanViewExample example = new EmpPlanViewExample();
        example.createCriteria().andEmpPlanIdEqualTo(emPlanId);
        List<EmpPlanView> empPlanViewList = empPlanViewMapper.selectByExample(example);
        EmpPlanView employeePlan= null;
        if(empPlanViewList != null && empPlanViewList.size()>0){
            for(EmpPlanView employeePlanTemp:empPlanViewList){
                if(employeePlanTemp.getStartDate() != null && new Date().compareTo(employeePlanTemp.getStartDate())<0){
                    continue;
                }
                if(employeePlanTemp.getEndDate() != null && new Date().compareTo(employeePlanTemp.getEndDate())>0){
                    continue;
                }
                employeePlan = employeePlanTemp;
            }

            return employeePlan;
        }
        return null;
    }

    /**
     * 获取被保人信息
     * @param employeeId
     * @return
     */
    public Employee getEmployeeInfo(String employeeId){
        EmployeeExample example = new EmployeeExample();
        example.createCriteria().andMemberControlNoEqualTo(employeeId);
        List<Employee> employeeList = employeeMapper.selectByExample(example);
        Employee employee = null;
        if(employeeList != null &&employeeList.size()>0){
            employee = employeeList.get(0);
        }
        return employee;
    }

    public Card getCardInfo(String empId){
        CardExample example = new CardExample();
        example.createCriteria().andMemberControlNoEqualTo(empId);
        List<Card> cardList = cardMapper.selectByExample(example);
        Card card = null;
        if(cardList != null && cardList.size()>0){
            card = cardList.get(0);
        }
        return card;
    }


    public List<CoverageDto> getCoverageList(Long planId,SymUser user,EmpPlanView employeePlan,Employee employee) throws Exception{
        List<CoverageDto> coverageVoList = new ArrayList<>();
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("corpCode",employeePlan.getCorpCode());
        paramMap.put("planCode",employeePlan.getPlanCode());
        List<CoverageVo> coverages = benefitVoMapper.getCoverageList(paramMap);
        List<CovCondition> covConditionList = new ArrayList<>();
        Corporate corporate = corporateMapper.selectByPrimaryKey(employeePlan.getCorpCode());
        Boolean isDirNetWork = isValidNetWork(paramMap,user,corporate);
        if(coverages != null && coverages.size() >0){
            for(CoverageVo coverageVo:coverages){
                coverageVo.setCurrcd(corporate.getCurrcd());
                coverageVo.setNetworkCode(corporate.getNetworkCode());
                CoverageDto coverageDto = new CoverageDto();
                coverageDto.setLoaDesc(coverageVo.getLoaDesc() == null ?"":coverageVo.getLoaDesc().toString()+"责任");
                if(!isDirNetWork){
                    coverageDto.setCashlessFacility("无直付，不是定点网络医院");
                    coverageDto.setAmtIncurred("无");
                    coverageDto.setCompensationRatio("无");
                    coverageDto.setDeductible("无");
                    coverageDto.setWatingPeroid("无");
                    coverageDto.setEdcMax("无");
                    coverageDto.setLimitTime("无");
                    coverageDto.setPreauthRequest("无");
                    coverageVoList.add(coverageDto);
                    continue;
                }
                paramMap.put("coverageCode",coverageVo.getCoverageCode());
                //是否支付
                String cashlessFacility = isCashlessFacility(coverageVo,user,employeePlan,covConditionList,paramMap);
                coverageDto.setCashlessFacility(cashlessFacility);
                String edcMax = getEdcmaxValueInfo(coverageVo);//刷卡上线
                coverageDto.setEdcMax(edcMax);
                String preRequest = getPreauthRequest(coverageVo);
                coverageDto.setPreauthRequest(preRequest);
                String amtIncurred = getAmtIncurred(coverageVo,paramMap,employee);
                coverageDto.setAmtIncurred(amtIncurred);
                String dedutible = getDeductible(coverageVo,covConditionList,employee);
                coverageDto.setDeductible(dedutible);
                String limitTime = getLimitTime(coverageVo,employee,covConditionList,coverages,paramMap);
                coverageDto.setLimitTime(limitTime);
                String waittime = getWaitTimeInfo(employee,coverageVo);
                coverageDto.setWatingPeroid(waittime);
                Map compenMap = getCompensationRatio(coverageVo,employee,covConditionList);
                Object compensationRatio = compenMap.get("1");//1 页面展示内容 2 比例后台传值
                if(compensationRatio != null){
                    coverageDto.setCompensationRatio(compensationRatio.toString());
                }else{
                    coverageDto.setCompensationRatio("");
                }
                String radio = "0";
                if(compenMap.get("2") != null){
                    radio = (String)compenMap.get("2");
                }
                //取费用列表
                coverageVoList.add(coverageDto);
            }
        }
        return coverageVoList;
    }

    /**
     * 判断是否是定点网络医院
     * @param paramMap
     * @param user
     * @param corporate
     * @return
     */
    public Boolean isValidNetWork(Map<String, Object> paramMap, SymUser user,Corporate corporate){
        String  providerCode ="";
        String networkCode = corporate.getNetworkCode();
        //查找登录人是否在被保人网络内
        SymUsrcoExample symUsrcoExample = new SymUsrcoExample();
        symUsrcoExample.createCriteria().andUsridEqualTo(user.getUsrid());
        List<SymUsrco> symUsrcoList = symUsrcoMapper.selectByExample(symUsrcoExample);
        if(symUsrcoList!= null && symUsrcoList.size()>0){
            providerCode = symUsrcoList.get(0).getStncd();
        }
        paramMap.put("networkCode",networkCode);
        paramMap.put("providerCode",providerCode);
        List<NetworkProvider> networkProviders = benefitVoMapper.getNetworkInBynetWorkCode(paramMap);
        if(networkProviders.size() == 0){
            return Boolean.FALSE;
        }
        //判断网络医院类型
        String netWorkType= networkProviders.get(0).getProviderType();

        Provider provider = providerMapper.selectByPrimaryKey(providerCode);
        if(StringUtils.isBlank(netWorkType)){
            netWorkType = provider.getProviderType();
        }
        //国家
        String counntry =provider.getCountry();//医院对应的国家
        if(StringUtils.isBlank(counntry)){
            counntry = "ZZZ";
        }
        paramMap.put("providerType",netWorkType);
        paramMap.put("country",counntry);
        return Boolean.TRUE;
    }
    /**
    /**
     * 判断是否享受直付
     * @param coverageVo
     * @param user
     * @param employeePlan
     * @return
     */
    private String isCashlessFacility(CoverageVo coverageVo,SymUser user,EmpPlanView employeePlan,List<CovCondition> covConditionList,Map paramMap){
        //判断是否开放直付
        String cashlessFacility="享受直付";
        Integer limitNum = benefitVoMapper.getLimitProviderCode(paramMap);
        if(limitNum.intValue()>0){
            cashlessFacility = "无直付";
            return cashlessFacility;
        }

        covConditionList = benefitVoMapper.getCovConditionInfo(paramMap);
        if(covConditionList != null && covConditionList.size()>0){
            for(CovCondition covCondition:covConditionList){
                if(covCondition.getLimitCode().startsWith("07")&&covCondition.getMaxValue().equals(BigDecimal.ZERO)){
                    cashlessFacility = "无直付";
                    break;
                }
            }
        }
        return cashlessFacility;
    }
    //直付刷卡上限
    public String getEdcmaxValueInfo(CoverageVo coverageVo){
        String edcMax ;//刷卡上线
        if(coverageVo.getEdcmaxValue() == null||coverageVo.getEdcmaxValue().intValue() ==0){
            edcMax ="未设定直付上限";
        }else{
            edcMax = coverageVo.getEdcmaxValue() + coverageVo.getCurrcd();
        }
        return edcMax;
    }
    /**
     * 事先授权说明
     * @param coverageVo
     * @return
     */
    public String getPreauthRequest(CoverageVo coverageVo){
         String   preAuthRequest ="";
         if(StringUtils.equals("Y",coverageVo.getPreauthRequired()) && coverageVo.getPreauthRequiredAmtIncurred().equals(BigDecimal.ZERO)){
             preAuthRequest = String.format("需要事先授权，授权有效限为%s天",coverageVo.getPrevalidity());
         }else if(StringUtils.equals("Y",coverageVo.getPreauthRequired()) && coverageVo.getPreauthRequiredAmtIncurred().compareTo(BigDecimal.ZERO)>0){
             preAuthRequest = String.format("就诊发生金额到达%s%s需要事先授权，授权有效限为%s天",coverageVo.getPreauthRequiredAmtIncurred().toString(),coverageVo.getCurrcd(),coverageVo.getPreauthValidity().toString());
         }else{
             CoverageKey coverageKey = new CoverageKey();
             BeanUtils.copyProperties(coverageVo,coverageKey);
             List<Benefit> benefitList = benefitVoMapper.getBenefitList(coverageKey);
             if(benefitList != null && benefitList.size() >0){
                 for(Benefit benefit:benefitList){
                     preAuthRequest +=benefit.getLoaDesc()+",";
                 }
             }
             if(StringUtils.isNotBlank(preAuthRequest)){
                 preAuthRequest = preAuthRequest.substring(0,preAuthRequest.length()-1)+ "需要事先授权".replaceAll(" ","");
             }else{
                 preAuthRequest ="无";
             }
         }
        return preAuthRequest;
    }

    /**
     * 年限额
     * @param coverageVo
     * @param paramMap
     * @return
     */
    public String getAmtIncurred(CoverageVo coverageVo,Map paramMap,Employee employee) throws Exception{
        String amtIncurred = "";
        List<CovCondition> conditionList= benefitVoMapper.getCovConditionInfoAmnt(paramMap);
        if(conditionList.size()==0){
            PlanConditionExample planConditionExample = new PlanConditionExample();
            planConditionExample.createCriteria()
                    .andPlanCodeEqualTo(coverageVo.getPlanCode())
                    .andLimitCodeEqualTo("0100")
                    .andFrequencyCodeEqualTo((short)8);
            List<PlanCondition> conditions = planConditionMapper.selectByExample(planConditionExample);
            if(conditions != null && conditions.size()>0){
                amtIncurred = conditions.get(0).getMaxValue().toString()+coverageVo.getCurrcd();
                return amtIncurred;
            }else{
                return "无";
            }

        }
        Integer flagCount =0;
        Integer attchCount =0;
        for(CovCondition covCondition:conditionList){
            if("04".equalsIgnoreCase(covCondition.getIfcondType())){
                BigDecimal birDecal = new BigDecimal(DateUtils.getAgeByBirth(employee.getDateOfBirth()));
                if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())<=0 &&
                        birDecal.compareTo(covCondition.getIfcondLeval().setScale(1,BigDecimal.ROUND_UP))<=0 &&
                        covCondition.getIfcondGeval().compareTo(birDecal)<=0
                ){
                    amtIncurred = covCondition.getMaxValue().toString()+coverageVo.getCurrcd();
                    return amtIncurred;
                }
                if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())> 0 &&
                        (birDecal.compareTo(covCondition.getIfcondLeval())<=0 ||birDecal.compareTo(covCondition.getIfcondGeval())>=0)){
                    amtIncurred = covCondition.getMaxValue().toString()+coverageVo.getCurrcd();
                    return amtIncurred;
                }
                continue;
            }
            if(covCondition.getMaxValue().equals(new BigDecimal("9"))){
                flagCount++;
            }

            if(StringUtils.isBlank(covCondition.getAttachCode()) && StringUtils.isBlank(covCondition.getIfcondType())){
                attchCount++;
            }
        }
        if(conditionList.size() == flagCount.intValue()){
            PlanConditionExample planConditionExample = new PlanConditionExample();
            planConditionExample.createCriteria()
                    .andPlanCodeEqualTo(coverageVo.getPlanCode())
                    .andLimitCodeEqualTo("0100")
                    .andFrequencyCodeEqualTo((short)8);
            List<PlanCondition> conditions = planConditionMapper.selectByExample(planConditionExample);
            if(conditions != null && conditions.size()>0){
                amtIncurred = conditions.get(0).getMaxValue().toString()+coverageVo.getCurrcd();
                return amtIncurred;
            }else{
                return "无";
            }
        }
        if(attchCount.intValue() == conditionList.size()){
            amtIncurred = conditionList.get(conditionList.size()-1).getMaxValue().toString()+coverageVo.getCurrcd();
            return amtIncurred;
        }else{
            return "请咨询客服";
        }
    }

    /**
     * 获取免赔额
     * @param coverageVo
     * @param covConditionList
     * @return
     */
    public String getDeductible(CoverageVo coverageVo,List<CovCondition> covConditionList,Employee employee) throws Exception{
        String Deductible="无";
        for(CovCondition covCondition:covConditionList){
            if("2500".equalsIgnoreCase(covCondition.getLimitCode())&& "8".equals(covCondition.getFrequencyCode())){
                Deductible = "每笔理赔"+covCondition.getMaxValue()+coverageVo.getCurrcd();
                break;
            }
            if("24,26,28".indexOf(covCondition.getLimitCode().substring(0,2))>0 && "8".equals(covCondition.getFrequencyCode())){
                if("04".equalsIgnoreCase(covCondition.getIfcondType())){
                    BigDecimal birDecal = new BigDecimal(DateUtils.getAgeByBirth(employee.getDateOfBirth()));
                    if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())<=0 &&
                            birDecal.compareTo(covCondition.getIfcondLeval().setScale(1,BigDecimal.ROUND_UP))<=0 &&
                            covCondition.getIfcondGeval().compareTo(birDecal)<=0
                    ){
                        Deductible = covCondition.getMaxValue().toString()+coverageVo.getCurrcd();
                        break;
                    }
                    if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())> 0 &&
                            (birDecal.compareTo(covCondition.getIfcondLeval())<=0 ||birDecal.compareTo(covCondition.getIfcondGeval())>=0)){
                        Deductible = covCondition.getMaxValue().toString()+coverageVo.getCurrcd();
                        break;
                    }
                    continue;
                }
            }
        }
        return Deductible;
    }
    /**
     * 判断等待期
     * @param employee
     * @param coverageVo
     * @return
     */
    public String getWaitTimeInfo(Employee employee,CoverageVo coverageVo){
        String waitPried ="";
        Date pridDate = DateUtils.addDate(employee.getMbaPolicyStartDate(),coverageVo.getWaitingPeriod());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 日期格式
        if(pridDate.compareTo(new Date())<0){
            waitPried ="无";
        }else{
            waitPried = String.format("%s天,至%s",String.valueOf(coverageVo.getWaitingPeriod()),df.format(pridDate));
        }
        return waitPried;
    }

    /**
     * 获取限制次数
     * @param coverageVo
     * @param covConditionList
     * @return
     */
    public String getLimitTime(CoverageVo coverageVo,Employee employee,List<CovCondition> covConditionList,List<CoverageVo> coverages,Map paramMap) throws Exception{
        String limitTime ="";
        StringBuilder sf = new StringBuilder();
        for(CovCondition covCondition:covConditionList){
            if(covCondition.getLimitCode().startsWith("27") && "8".equals(covCondition.getFrequencyCode())){
                //判被断险人的年龄是否在设定范围中
                if("04".equalsIgnoreCase(covCondition.getIfcondType())){
                    BigDecimal birDecal = new BigDecimal(DateUtils.getAgeByBirth(employee.getDateOfBirth()));
                    if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())<=0 &&
                            birDecal.compareTo(covCondition.getIfcondLeval().setScale(1,BigDecimal.ROUND_UP))<=0 &&
                            covCondition.getIfcondGeval().compareTo(birDecal)<=0
                    ){
                        limitTime = covCondition.getMaxValue().toString()+"次";
                    }
                    if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())> 0 &&
                            (birDecal.compareTo(covCondition.getIfcondLeval())<=0 ||birDecal.compareTo(covCondition.getIfcondGeval())>=0)){
                        limitTime = covCondition.getMaxValue().toString()+"次";
                    }
                    if(covCondition.getConditionType() != null){
                        List<String> list = new ArrayList<>();
                        for(CoverageVo coverageVo1:coverages){
                            if(!StringUtils.equals(coverageVo.getCoverageCode(),coverageVo1.getCoverageCode())
                            ){
                                list.add(coverageVo1.getCoverageCode());
                            }
                        }
                        paramMap.put("list",list);

                        List<CovCondition> covConditions = benefitVoMapper.getConditionCoverName(paramMap);
                        if(covConditions != null && covConditions.size()>0){
                            for(CovCondition condition2:covConditions){
                                sf.append(condition2.getLoaDesc()).append(",");
                            }
                        }
                    }
                }
                continue;
            }
        }
        if(StringUtils.isBlank(limitTime)){
            limitTime = "无";
            return limitTime;
        }
        if(StringUtils.isNotBlank(sf.toString())){
            return limitTime + "与"+sf.toString().substring(0,sf.toString().length()-1);
        }else{
            return limitTime;
        }
    }

    public Map getCompensationRatio(CoverageVo coverageVo,Employee employee,List<CovCondition> covConditionList) throws Exception{
        BigDecimal compensationRatio;
        Map<String,Object> showMap = new HashMap<>();
        for(CovCondition covCondition:covConditionList){
            if(StringUtils.isBlank(covCondition.getProviderType()) &&
                    StringUtils.isBlank(covCondition.getCountry())&&
            StringUtils.isBlank(covCondition.getIfcondType())&&
                    covCondition.getElsecondCoinsrcap().equals(BigDecimal.ZERO)){
                continue;
            }
            if(covCondition.getLimitCode().startsWith("07") && "10".equals(covCondition.getFrequencyCode())){
                if("04".equalsIgnoreCase(covCondition.getIfcondType())){
                    BigDecimal birDecal = new BigDecimal(DateUtils.getAgeByBirth(employee.getDateOfBirth()));
                    if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())<=0 &&
                            birDecal.compareTo(covCondition.getIfcondLeval().setScale(1,BigDecimal.ROUND_UP))<=0 &&
                            covCondition.getIfcondGeval().compareTo(birDecal)<=0
                    ){
                        compensationRatio = covCondition.getElsecondMaxValue();
                    }
                     else if(covCondition.getIfcondGeval().compareTo(covCondition.getIfcondLeval())> 0 &&
                            (birDecal.compareTo(covCondition.getIfcondLeval())<=0 ||birDecal.compareTo(covCondition.getIfcondGeval())>=0)){
                        compensationRatio = covCondition.getElsecondMaxValue();
                    }else{
                        compensationRatio = covCondition.getMaxValue();
                    }
                    showMap.put("2",compensationRatio);
                    if(compensationRatio.equals(BigDecimal.ZERO)){
                        showMap.put("1","0%");
                        showMap.put("2","0");
                        return showMap;
                    }
                    continue;
                }
                if("11".equals(covCondition.getIfcondType())||"16".equals(covCondition.getIfcondType())){
                    showMap.put("1","未通过授权，整笔理赔按费用赔付的基础上按"+covCondition.getElsecondMaxValue()+"% 赔付");
                }
                compensationRatio = covCondition.getMaxValue();
                showMap.put("2",compensationRatio);
            }
        }
        return showMap;
    }
    
    
    /**
     * 获取责任下的 费用信息
     * 
     */
    public List<BenefitInfoDto>  getBenefitinfo(Map<String,Object> maps){
    	List<BenefitInfoDto> redultList=new ArrayList<BenefitInfoDto>();
    	String compensationRatio=(String) maps.get("compensationRatio");//赔付比例
    	List<Map<String, Object>> list=benefitVoMapper.qiuerybenefitinfoList2(maps);
    	List<Map<String,Object>> countList=benefitVoMapper.qiuerybenefitinfoListCount(maps);
    	Map<String,Integer>  mapCount=getCountMap(countList);
    	System.out.println("====2222====="+list.size());
    	if(list!=null&&list.size()>0){
    		for (Map<String, Object> map : list) {
    			BenefitInfoDto   benfit=new BenefitInfoDto();
    			String benefitCode=(String) map.get("benefitCode");
    			String loaDesc=(String) map.get("loaDesc");
    			BigDecimal waitingPeriod= (BigDecimal)map.get("waitingPeriod");
    			String preauthrequird=(String) map.get("preauthrequird");
    			String limitCode=(String) map.get("limitCode");
    			BigDecimal conditionType=(BigDecimal) map.get("conditionType");
    			BigDecimal frequenctCode=(BigDecimal) map.get("frequenctCode");
    			BigDecimal maxValue=(BigDecimal) map.get("MaxValue");
    			 
    			limitCode=limitCode+"*"+frequenctCode;
    			benfit.setLoaDesc(loaDesc);
    			benfit.setWaitingPeriod(waitingPeriod+"");
    			benfit.setPreauthRequired(preauthrequird);
    			benfit.setLimitCode(limitCode);
    			benfit.setConditionType(conditionType==null?"0":conditionType+"");
    			String conditionTypes=conditionType==null?"0":conditionType+"";
    			benfit.setMaxValue(maxValue+"");
    			if("Y".equals(preauthrequird)){
    				benfit.setPreauthrequiName("该项费用需要事先授权");
    			}
    			Map<String,Object> pram=new HashMap<String, Object>();
    			pram.put("corpCode", maps.get("corpCode"));
    			pram.put("planCode", maps.get("planCode"));
    			pram.put("coverageCode", maps.get("coverageCode"));
    			pram.put("benefitCode", benefitCode);
    			if("0700*10".equals(limitCode)||"0701*10".equals(limitCode)){
    				BigDecimal ratio=new BigDecimal(0);
    				if(compensationRatio!=null&&!"".equals(compensationRatio)){
    					ratio=new BigDecimal(Long.valueOf(compensationRatio)/100);
    				}
    				benfit.setMaxValue((maxValue.multiply(ratio)+""));
    			}
    				
    			//获取已赔付赔付次数
    			Integer count=mapCount.get(benefitCode);
    			if("0100*17".equals(limitCode)||"0600*11".equals(limitCode)||"0800*10".equals(limitCode)||"3000*8".equals(limitCode)){
    				continue;
    			}else{
    				setLimitName(limitCode,benfit);
    			}
    			count=count==null?0:count;
    			setLimitNameDesc(conditionTypes,count,benfit,maxValue);
    		
    			redultList.add(benfit);
			}
    		
    	}
    	
    	return redultList;
    }
    
    
    
    private  Map<String,Integer>  getCountMap(List<Map<String,Object>> list){
    	Map<String,Integer> resultMap=new HashMap<String, Integer>();
    	if(list!=null&&list.size()>0){
    		for (Map<String, Object> map : list) {
    			Integer count=(Integer) map.get("count");
    			String benefitCode= (String) map.get("benefitCode");
    			resultMap.put(benefitCode, count);
			}
    	}
    	
        return resultMap;
    }
    
    private void  setLimitNameDesc(String conditionTypes,Integer count,BenefitInfoDto   benfit,BigDecimal maxValue){
    	if((conditionTypes!=null&&!"".equals(conditionTypes))&&Long.valueOf(conditionTypes)>50){
			benfit.setLimitNameDesc(maxValue+"次"+","+"已使用"+count+"次"+","+"独立限额");
		}else if((conditionTypes!=null&&!"".equals(conditionTypes))&&Long.valueOf(conditionTypes)<=50){
			benfit.setLimitNameDesc(maxValue+"次"+","+"已使用"+count+"次"+","+"共享限额");
		}else{
			benfit.setLimitNameDesc(maxValue+"次"+","+"已使用"+count+"次");
		}
     }
    
    
    @SuppressWarnings("unused")
	private  void setLimitName(String limitCode,BenefitInfoDto   benfit){
    	Map<String ,Object> map=getLimitNameMap();
    	String limitName=(String) map.get(limitCode);
    	benfit.setLimitName(limitName);
    }
    
    private Map<String ,Object>  getLimitNameMap(){
    	Map<String ,Object> map =new HashMap<String, Object>();
    	map.put("0100*10", "次限额");
    	map.put("0100*1", "日限额");
    	map.put("0100*19", "日限额");
    	map.put("0100*11", "每一疾病限额");
    	map.put("0100*12", "终生限额");
    	map.put("0100*17", ""); //跳过
    	map.put("0100*20", "保单限额");
    	map.put("0100*8 ", "年限额");
    	map.put("0101*8", "年限额");
    	map.put("0300*11", "每一病症住院天数");
    	map.put("0301*11", "每一病症住院天数");
    	map.put("0300*8", "年住院天数");
    	map.put("0301*8", "年住院天数");
    	map.put("0300*12", "终生住院天数");
    	map.put("0301*12", "终生住院天数");
    	map.put("0600*11", "");//跳过
    	map.put("0700*10", "赔付比例");
    	map.put("0701*10", "赔付比例");
    	map.put("0800*10", "");//跳过
    	map.put("2700*8", "限次");
    	map.put("2700*18", "每三年限次");
    	map.put("2700*16", "每两年限次");
    	map.put("2700*12", "终生限次");
    	map.put("2702*8", "限次");
    	map.put("3000*8", "");//跳过
    	return map;
    	
    }
    
    
    public String checkIdCard(String json,String url,String type){
    	 String paramData= "pstJson="+json;
    	String res=HttpClientUtil.sendUserPost(url,paramData);
    	return  res;
    }

    @Autowired
    private MedicialService medicialService;

    /**
     * 获取门诊列表
     * @param user
     * @return
     */
    public List<Map<String,Object>> getCaseInfoList(SymUser user){
        Map<String,Object> map = medicialService.getTerminalId(user);
        List<Map<String,Object>> caseInfoList = benefitVoMapper.getCaseInfoList(map.get(""));
        if(caseInfoList != null && caseInfoList.size()>0){
            for(Map<String,Object> caseMap :caseInfoList){
                if(caseMap.get("DEP_CODE")==null){
                    caseMap.put("DEP_CODE","");
                }
                if(caseMap.get("DEP_NAME")==null){
                    caseMap.put("DEP_NAME","");
                }
                if(caseMap.get("COVERAGE_CODE")==null){
                    caseMap.put("COVERAGE_CODE","");
                }
                if(caseMap.get("TOP_DEP_NAME")==null){
                    caseMap.put("TOP_DEP_NAME","");
                }
            }
        }
        return caseInfoList;
    }
}


